﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Demos.util;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DevExpress.XtraReports.UI;
using DXApplicationTangche.report;
using DXApplicationTangche.UC.下单MTO.Service;
using DXApplicationTangche.UC.成品出库单.service;
using DXApplicationTangche.UC.抖音出入库.Service;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using DXApplicationTangche.UC.订单一览新.销售确认单;
using DXApplicationTangche.UC.钉钉;
using DXApplicationTangche.UC.面料审核.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.抖音出入库
{
    public partial class Form抖音订单比对出库 : DevExpress.XtraEditors.XtraForm
    {
        private List<StockInOutDetailDTO> StockInOutDetailDTOs = new List<StockInOutDetailDTO>();
        private List<DouyinRequestDTO> douyinRequestDTOs一览显示 = new List<DouyinRequestDTO>();

        private List<DouyinRequestDTO> douyinRequestDTOs抖音扫码 = new List<DouyinRequestDTO>();
        private List<DouyinRequestDTO> douyinRequestDTOs抖音扫码一览 = new List<DouyinRequestDTO>();


        public Form抖音订单比对出库()
        {
            InitializeComponent();
            this.searchLookUpEdit选择款式.Properties.DataSource = 抖音Service.getDouyinMaterialAll();
        }
        #region
        private void searchLookUpEdit选择款式_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear选择款式Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit选择款式View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit选择款式.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    String refBarcode = this.searchLookUpEdit选择款式.Properties.View.GetRowCellValue(rowHandle, "refBarcode").ToString();
                    String styleNameCn = this.searchLookUpEdit选择款式.Properties.View.GetRowCellValue(rowHandle, "styleNameCn").ToString();
                    String materialCode = this.searchLookUpEdit选择款式.Properties.View.GetRowCellValue(rowHandle, "materialCode").ToString();
                    DouyinRequestDTO douyinRequestDTO选择 = new DouyinRequestDTO(refBarcode, styleNameCn, materialCode);
                    new Form抖音填写需求数量(douyinRequestDTO选择).ShowDialog();
                    foreach(DouyinRequestDTO douyinRequestDTO in this.douyinRequestDTOs一览显示)
                    {
                        try
                        {
                            if (douyinRequestDTO选择.refBarcode.Equals(douyinRequestDTO.refBarcode) && douyinRequestDTO选择.styleSizeCd.Equals(douyinRequestDTO.styleSizeCd))
                            {
                                MessageBox.Show("款式已存在，请勿重复选择");
                                return;
                            }
                        }
                        catch { }
                    }
                    this.douyinRequestDTOs一览显示.Add(douyinRequestDTO选择);
                    this.douyinRequestDTOs抖音扫码.Add(douyinRequestDTO选择);
                    this.gridControl抖音扫码.DataSource = this.douyinRequestDTOs抖音扫码;
                    this.gridView抖音扫码.RefreshData();
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear选择款式Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit选择款式.ToolTip = null;
            searchLookUpEdit选择款式.EditValue = null;
        }
        private void searchLookUpEdit选择款式_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            //if (null != e.Value)
            //{
                
            //}
        }
        #endregion

        private void 修改数量ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String refBarcode = this.gridView抖音扫码.GetRowCellValue(this.gridView抖音扫码.FocusedRowHandle, "refBarcode").ToString();
            new Form抖音填写需求数量(this.douyinRequestDTOs抖音扫码[this.gridView抖音扫码.FocusedRowHandle], "修改数量").ShowDialog();
            this.gridControl抖音扫码.DataSource = this.douyinRequestDTOs抖音扫码;
            this.gridView抖音扫码.RefreshData();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.douyinRequestDTOs抖音扫码.Remove(this.douyinRequestDTOs抖音扫码[this.gridView抖音扫码.FocusedRowHandle]);
            this.gridControl抖音扫码.DataSource = this.douyinRequestDTOs抖音扫码;
            this.gridView抖音扫码.RefreshData();
        }

        private void textBox扫码_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (!String.IsNullOrWhiteSpace(textBox扫码.Text))
                {                   
                    BlnResult blnResult = 
                        this.判断款式(
                            this.douyinRequestDTOs抖音扫码, 
                            MTO订单Service.getOrderCommitDtoByBarcode(textBox扫码.Text.Trim()), //  order commit dto 
                            this.textBox扫码.Text.Trim()
                        );
                    OrderCommitDTO返回 orderCommitDTO = MTO订单Service.getOrderCommitDtoByBarcode(textBox扫码.Text.Trim());
                    if (!blnResult.if款式)
                    {
                        MessageBox.Show("扫描成衣款式信息与已选款式不符");
                        return;
                    }
                    if (blnResult.if条码存在)
                    {
                        MessageBox.Show("条码已存在，请勿重复出库");
                        return;
                    }
                    else {
                        //  条码不重复，刷新一览
                        this.gridView抖音扫码.RefreshData();
                        try { orderCommitDTO.styleSizeCd = orderCommitDTO.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                        this.StockInOutDetailDTOs.Add(new StockInOutDetailDTO(this.textBox扫码.Text.Trim(), orderCommitDTO.styleNameCn, orderCommitDTO.materialCode, orderCommitDTO.styleSizeCd));
                    }
                    this.textBox扫码.Clear();
                }
                else
                {
                    MessageBox.Show("成衣码为空");
                    this.textBox扫码.Clear();
                    return;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="douyinRequestDTOs抖音扫码"></param>
        /// <param name="orderCommitDTO返回"></param>
        /// <param name="barcode"></param>
        /// <returns>款式是否匹配</returns>
        private BlnResult 判断款式(List<DouyinRequestDTO> douyinRequestDTOs抖音扫码,OrderCommitDTO返回 orderCommitDTO返回,String barcode) {
            
            foreach (DouyinRequestDTO douyinRequestDTO in douyinRequestDTOs抖音扫码)
            {
                //判断扫描成衣码款式、面料号是否相符
                //款式匹配
                if (douyinRequestDTO.styleNameCn.Equals(orderCommitDTO返回.styleNameCn)
                    && douyinRequestDTO.materialCode.Equals(orderCommitDTO返回.materialCode))
                {
                    bool if条码存在 = this.判断条码(douyinRequestDTO, barcode);  //  条码是否重复
                    
                    //  匹配上款式和面料的后续逻辑
                    //已完成数量自增
                    if (!if条码存在)
                    {
                        douyinRequestDTO.douyinStockOutDetailDTOs.Add(new DouyinStockOutDetailDTO(barcode, orderCommitDTO返回.styleNameCn, orderCommitDTO返回.materialCode, orderCommitDTO返回.styleSizeCd));
                        douyinRequestDTO.stockOutQuantity++;
                        this.douyinRequestDTOs抖音扫码一览.Add(douyinRequestDTO);
                    }
                    //符合款式 为true
                    return new BlnResult(
                            true,
                             if条码存在
                        );
                }
            }
            return new BlnResult();
        }

        /// <summary>
        /// 判断条码是否重复，如果不重复，加入到待出库一览中
        /// </summary>
        /// <param name="douyinRequestDTO"></param>
        /// <param name="barcode"></param>
        private bool 判断条码(DouyinRequestDTO douyinRequestDTO, String barcode) {
            //循环detail
            //barcode数量大于1时判断是否重复
            if (douyinRequestDTO.douyinStockOutDetailDTOs.Count > 0)
            {
                foreach (DouyinStockOutDetailDTO douyinStockOutDetailDTO in douyinRequestDTO.douyinStockOutDetailDTOs)
                {
                    if (barcode.Equals(douyinStockOutDetailDTO.barcode))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        private void simpleButton抖音出库_Click(object sender, EventArgs e)
        {
            //保存抖音出库单
            抖音Service.saveDouyinRequest(this.douyinRequestDTOs抖音扫码);
            //report出库单
            XtraReport抖音出库单 report = new XtraReport抖音出库单(this.douyinRequestDTOs抖音扫码);
            StockInOutTitleDTO 出库单DTO = new StockInOutTitleDTO(
                                        "",
                                        DateTime.Now,
                                        "0",
                                        "抖音出库",
                                        "fengjinyue01",
                                        "冯金月",
                                        0,
                                        "8",
                                        "退回冻结存货",
                                        "抖音出库"
                                    );
            //出库单时间
            String stockTime = DateTime.Now.ToString();
            try { stockTime = stockTime.Replace(" ", "").Replace("/", "").Replace(":", ""); } catch { }
            String id = "douyinStockOut_" + stockTime;
            //钉钉
            try
            {
                //  上传pdf:
                string saveDirectoryPath = @"pdf\";
                //  1.先本地生成文件
                report.ExportToPdf(saveDirectoryPath + id + @".pdf");
                //  2.上传本地文件
                String re = 成品出库单Service.uploadStockInOutFileByCode(id, saveDirectoryPath + id + @".pdf");
                //  3.删除本地文件
                //File.Delete(saveDirectoryPath + re);
                //  钉钉
                钉钉Service.start(new ProcessStartDTO(调拨出库类型Consts.抖音出库, 出库单DTO, re).build("17866554337", 409162359L));
            }
            catch (Exception ex)
            {
                MessageBox.Show("服务连接失败，钉钉发送失败，请联系李金键");
            }
            ReportPrintTool tool = new ReportPrintTool(report);
            tool.ShowPreview();
        }

        private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column.Name.Equals("gridColumn尺码3"))
            {
                if (!String.IsNullOrWhiteSpace(e.DisplayText))
                {
                    try { e.DisplayText = e.DisplayText.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                }
                else
                {
                    e.DisplayText = "样衣无尺寸定义";
                }
            }
        }
    }

    
}
